package com.example.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.util.Result;
import com.example.process.ProcessTemplate;
import com.example.process.service.ProcessTemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/process/processTemplate")
public class ProcessTemplateController {

    @Autowired
    private ProcessTemplateService processTemplateService;

    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page",value = "当前页码",required = true)
                        @PathVariable Long page,
                        @ApiParam(name = "limit",value = "每页记录数",required = true)
                        @PathVariable Long limit){
        Page<ProcessTemplate> page1 = new Page<>(page,limit);
        IPage<ProcessTemplate> page2 = processTemplateService.selectPage(page1);
        return Result.ok(page2);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.ok(processTemplate);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result save(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result updateById(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        processTemplateService.removeById(id);
        return Result.ok();
    }

//    @PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')") //加了这个前端请求不到
    @ApiOperation(value = "上传流程定义")
    @PostMapping("/uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file){
        try {
            String path = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();

            String filename = file.getOriginalFilename();
            // 上传目录
            File tempFile = new File(path + "/processes");
            // 判断目录是否存着
            if(!tempFile.exists()){
                tempFile.mkdirs(); //创建目录
            }
            //创建空文件用于写入文件
            File imageFile = new File(path + "/processes/" + filename);
            //保存文件流到本地
            file.transferTo(imageFile);

            Map<String,Object> map = new HashMap<>();
            //根据上传地址后续部署流程定义，文件名称为流程定义的默认key
            map.put("processDefinitionPath","processes/" + filename);
            map.put("processDefinitionKey",filename.substring(filename.lastIndexOf(".")));
            return Result.ok(map);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return Result.fail("路径获取失败");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传失败");
        }

    }

//    @PreAuthorize("hasAuthority('bnt.processTemplate.publish')")
    @ApiOperation(value = "发布")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id){
        processTemplateService.publish(id);
        return Result.ok();
    }






}
